#include <iostream>
#include <vector>

using namespace std;

struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode* left, TreeNode* right)
        : val(x), left(left), right(right) {}
};

TreeNode* makeTree(vector<int>& nums, int left, int right){
    if(left > right) return nullptr;
    if(left == right){
        TreeNode* n = new TreeNode(nums[left]);
        return n;
    }
    int maxv = -1;
    int pos = left;
    for(int i=left;i<=right;i++){
        if(nums[i] > maxv){
            pos = i;
            maxv = nums[i];
        }
    }
    TreeNode* root = new TreeNode(maxv);
    root->left = makeTree(nums, left, pos-1);
    root->right = makeTree(nums, pos+1, right);
    return root;
}

TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
    return makeTree(nums, 0, nums.size()-1);
}
